package com.zhuoya.healthcard.util;

import android.os.*;
import android.view.*;
import android.util.*;

public class ScannerKeyEventHelper {
    private boolean mCaps;
    private final Handler mHandler;
    private OnScanSuccessListener mOnScanSuccessListener;
    private final Runnable mScanningFinishedRunnable;
    private StringBuffer mStringBufferResult;

    public ScannerKeyEventHelper(final OnScanSuccessListener mOnScanSuccessListener) {
        this.mOnScanSuccessListener = mOnScanSuccessListener;
        this.mStringBufferResult = new StringBuffer();
        this.mHandler = new Handler();
        this.mScanningFinishedRunnable = new Runnable() {
            @Override
            public void run() {
                ScannerKeyEventHelper.this.performScanSuccess();
            }
        };
    }

    private void checkLetterStatus(final KeyEvent keyEvent) {
        final int keyCode = keyEvent.getKeyCode();
        if (keyCode == 59 || keyCode == 60) {
            if (keyEvent.getAction() == 0) {
                this.mCaps = true;
                return;
            }
            this.mCaps = false;
        }
    }

    private char getInputCode(final KeyEvent keyEvent) {
        final int keyCode = keyEvent.getKeyCode();
        final StringBuilder sb = new StringBuilder();
        sb.append(keyCode);
        sb.append("");
        Log.i("TAGKEYCODE", sb.toString());
        if (keyCode >= 29 && keyCode <= 54) {
            int n;
            if (this.mCaps) {
                n = 65;
            } else {
                n = 97;
            }
            return (char) (n + keyCode - 29);
        }
        if (keyCode >= 7 && keyCode <= 16) {
            if (!this.mCaps) {
                return (char) (keyCode + 48 - 7);
            }
            switch (keyCode) {
                default: {
                    return ' ';
                }
                case 16: {
                    return '(';
                }
                case 15: {
                    return '*';
                }
                case 14: {
                    return '&';
                }
                case 13: {
                    return '^';
                }
                case 12: {
                    return '%';
                }
                case 11: {
                    return '$';
                }
                case 10: {
                    return '#';
                }
                case 9: {
                    return '@';
                }
                case 8: {
                    return '!';
                }
                case 7: {
                    return ')';
                }
            }
        } else {
            if (keyCode == 17) {
                return '*';
            }
            if (keyCode == 18) {
                return '#';
            }
            if (keyCode == 56) {
                return '.';
            }
            if (keyCode == 69) {
                char c;
                if (this.mCaps) {
                    c = '_';
                } else {
                    c = '-';
                }
                return c;
            }
            if (keyCode == 73) {
                char c2;
                if (this.mCaps) {
                    c2 = '|';
                } else {
                    c2 = '\\';
                }
                return c2;
            }
            if (keyCode == 74) {
                char c3;
                if (this.mCaps) {
                    c3 = ':';
                } else {
                    c3 = ';';
                }
                return c3;
            }
            if (keyCode == 76) {
                return '/';
            }
            if (keyCode != 77) {
                return ' ';
            }
            return '@';
        }
    }

    private void performScanSuccess() {
        final String string = this.mStringBufferResult.toString();
        final OnScanSuccessListener mOnScanSuccessListener = this.mOnScanSuccessListener;
        if (mOnScanSuccessListener != null) {
            mOnScanSuccessListener.onScanSuccess(string);
        }
        this.mStringBufferResult.setLength(0);
    }

    public void analysisKeyEvent(final KeyEvent keyEvent) {
        final int keyCode = keyEvent.getKeyCode();
        this.checkLetterStatus(keyEvent);
        if (keyEvent.getAction() == 0) {
            final char inputCode = this.getInputCode(keyEvent);
            if (inputCode != '\0') {
                this.mStringBufferResult.append(inputCode);
            }
            if (keyCode == 66) {
                this.mHandler.removeCallbacks(this.mScanningFinishedRunnable);
                this.mHandler.post(this.mScanningFinishedRunnable);
            }
        }
    }

    public void onDestroy() {
        this.mHandler.removeCallbacks(this.mScanningFinishedRunnable);
        this.mOnScanSuccessListener = null;
    }

    public interface OnScanSuccessListener {
        void onScanSuccess(final String p0);
    }
}
